<?php


namespace App\Models\Admin;


class AdminTender extends Base
{
    protected $table = 'admin_tender';
    protected $guarded = [];
    protected $dateFormat = 'U';

    public function site()
    {
        return $this->belongsTo(AdminTenderSite::class,'site_id','id');
    }

    public static function getList($data=[],$skip=0,$size=10)
    {
        $select=self::getWhere($data);
        $list=$select
            ->with([
                'site:id,name'
            ])
            ->orderBy('id','desc')
            ->select('id','title','url','site_id','created_at','status')
            ->skip($skip)->take($size)
            ->get()
        ->transform(function ($i)
        {
            $i->site_name=$i->site?$i->site->name:'';
            unset($i->site);
            return $i;
        });
        return $list;
    }

    public static function getWhere($data)
    {
        $select=self::where([]);
        if(!empty($data['title']))
        {
            $select=$select->where('title','like','%'.$data['title'].'%');
        }
        if(!empty($data['site_id']))
        {
            $select=$select->where('site_id',$data['site_id']);
        }
        if(!empty($data['start_time']) && !empty($data['end_time']))
        {
            $select = $select->whereBetween('created_at',date_assemble($data['start_time'],$data['end_time']));
        }
        return $select;
    }

    public static function getCount($data)
    {
        $select=self::getWhere($data);
        $count=$select->count();
        return $count;
    }
}
